package designpattern

import (
	"encoding/json"
	"fmt"
	"io/ioutil"

	"github.com/oliveagle/jsonpath"
)

func QueryTemplate[T any, V any](path string, callbackFn func(data T) (V, error)) (V, error) {
	var zero V
	jsonFile, err := ioutil.ReadFile("../config/Template_Query.json")
	if err != nil {
		return zero, err
	}

	config := make(map[string]interface{})
	err = json.Unmarshal(jsonFile, &config)
	if err != nil {
		return zero, err
	}
	res, err := jsonpath.JsonPathLookup(config, path)
	if t, ok := res.(T); ok {
		return callbackFn(t)
	} else {
		return zero, fmt.Errorf("No value found for path")
	}

}
